package cn.ffcs.bpmpvc.services.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jxl.Sheet;
import jxl.Workbook;

import cn.ffcs.bpm.model.DateControl;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.dao.AppBusinessReqDao;
import cn.ffcs.bpmpvc.model.BpmSwimlane;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.services.AppDataTranferService;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.ReqProgress;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.security.model.User;
import cn.ffcs.util.converter.DateUtil;

public class AppDataTranferServiceImpl implements AppDataTranferService {
	private AppBusinessReqDao appBusinessReqDao;
    private ProcessPvcService processPvcService;
    private TaskService taskService;
    private AppBusinessReqService appBusinessReqService;
    
    /**
     * 时间管控添加或修改
     */
    public boolean intoDateControl(HttpServletRequest request) {
    	String reqId=request.getParameter("reqId");
		BusinessRequest businessRequest=getAppBusinessReqService().getAppBusinessReqDao().getBusinessRequestById(Integer.parseInt(reqId));
		String processId=getProcessPvcService().getFlowPvcDao().getFlowProcessId(businessRequest.getProcinstId().toString());
		List<ProcessConfigDetail> processDetailList=getProcessPvcService().getFlowPvcDao().findAllProcessDetail(Integer.parseInt(processId));
		getAppBusinessReqDao().deleteDateControlByReqId(reqId);
		Date date=null;
		int n=0;
		for (int i = 0; i < processDetailList.size(); i++) {
			ProcessConfigDetail configDetail=processDetailList.get(i);
			String date_str=request.getParameter("process"+configDetail.getId());
			if(date_str!=null&&!"".equals(date_str)){
				if(n==0){
					date=DateUtil.strToDate(date_str, "yyyy-MM-dd");
					n++;
				}else{
					Date date2=DateUtil.strToDate(date_str, "yyyy-MM-dd");
					if(date2.before(date)){
						return false;
					}
					date=date2;
				}
			}
		}
		
		
		for (int i = 0; i < processDetailList.size(); i++) {
			ProcessConfigDetail configDetail=processDetailList.get(i);
			String date_str=request.getParameter("process"+configDetail.getId());
			if(date_str!=null&&!"".equals(date_str)){
				DateControl control=new DateControl();
				control.setReqId(reqId);
				control.setProcessConfigDetail(configDetail.getId().toString());
				control.setManagerDate(DateUtil.strToDate(date_str, "yyyy-MM-dd"));
				control.setOperateTime(new Date());
				getProcessPvcService().getFlowPvcDao().save(control);
			}
		}
		return true;
	}
    
    
    public void impDataFromExcel(){
       	try {
			Workbook book = Workbook.getWorkbook(new File("d:/2010年产品任务导入.xls"));
			Sheet[] sheets=book.getSheets();
			for (int i = 0; i < sheets.length; i++) {
				Sheet sheet =sheets[i];
//				System.out.println(sheet.getRows());;
//				System.out.println(sheet.getColumns());;
//				for (int j = 1; j <sheet.getRows(); j++) {
//					System.out.println("["+i+","+j+"]"+sheet.getCell(4, j).getContents());
//					String planTime=sheet.getCell(4, j).getContents();
//					Date planDate=null;
//					if(planTime!=null&&!"".equals(planTime)){
//						try {
//							//planDate=DateUtil.strToDate(planTime, "yyyy.MM.dd");
//							planDate=DateUtil.strToDate(planTime, "dd/MM/yyyy");
//						} catch (Exception e) {
//							
//						}
//						System.out.println(DateUtil.dateTimeToStr(planDate, "yyyy-MM-dd"));
//					}
//				}
				BusinessRequest businessRequest=null;
				ProcessConfig processConfig=null;
				ProcessConfigDetail processConfigDetail=null;
				for (int j = 1; j < sheet.getRows(); j++) {
					businessRequest=new BusinessRequest();
					businessRequest.setName(sheet.getCell(0,j).getContents());
					businessRequest.setContent(sheet.getCell(1,j).getContents());
					String startPerson=sheet.getCell(2,j).getContents();
					//System.out.println("///"+i+"///"+j+"//"+startPerson);
					Person startPersonObj=this.getAppBusinessReqDao().getPerson(startPerson);
					if(startPersonObj==null){
						continue;
					}
					Organization startOrganization=this.getAppBusinessReqDao().getOrgByPerson(startPersonObj.getId());
					businessRequest.setStartPerson(startPerson);
					businessRequest.setStartPersonObj(startPersonObj);
					businessRequest.setStartDept(startOrganization.getName());
					businessRequest.setStartDeptObj(startOrganization);
					
	
					String begin=sheet.getCell(3,j).getContents();
					if(begin.indexOf("~")==-1){
						char n=begin.charAt(begin.length()-1);
						String yyyy=begin.substring(0,4);
						if(n=='1'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-01-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-03-31"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(n=='2'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-04-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-06-30"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(n=='3'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-07-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-09-30"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(n=='4'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-10-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-12-31"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}
					}else{
						String str[]=begin.split("~");
						String start =str[0];
						String end=str[1];
						char s=start.charAt(start.length()-1);
						char e=end.charAt(end.length()-1);
						String yyyy=start.substring(0,4);
						if(s=='1'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-01-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(s=='2'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-04-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(s=='3'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-07-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(s=='4'){
							businessRequest.setBegin(DateUtil.strToDateTime(yyyy+"-10-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}
						yyyy=end.substring(0,4);
						if(e=='1'){
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-03-31"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(e=='2'){
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-06-30"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(e=='3'){
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-09-30"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}else if(e=='4'){
							businessRequest.setPlanTime(DateUtil.strToDateTime(yyyy+"-12-31"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						}
					}
					//businessRequest.setBegin(DateUtil.strToDateTime(begin+" 08:00:00","dd/MM/yyyy HH:mm:ss"));
					String planTime=sheet.getCell(4, j).getContents();
					Date planDate=null;
					if(planTime!=null&&!"".equals(planTime)){
						try {
							//planDate=DateUtil.strToDate(planTime, "yyyy.MM.dd");
							planDate=DateUtil.strToDate(planTime, "dd/MM/yyyy");
						} catch (Exception e) {
							
						}
						if(planDate!=null){
							businessRequest.setPlanTime(planDate);
						}
					}
					businessRequest.setReqCode(getAppBusinessReqService().getReqCode());
					String prodPerson=sheet.getCell(5, j).getContents();
					Person prodPersonObj=this.getAppBusinessReqDao().getPerson(prodPerson);
					Organization prodOrganization=this.getAppBusinessReqDao().getOrgByPerson(prodPersonObj.getId());
					businessRequest.setProdDept(prodOrganization);
					businessRequest.setProdPerson(prodPersonObj);
					businessRequest.setEnd(DateUtil.getMaxDate());
					String str_processConfig=sheet.getCell(6, j).getContents();
					String str_nowPerson=sheet.getCell(7,j).getContents();
					String str_processCongigDetail=sheet.getCell(8,j).getContents();
					
					Person nowPerson =this.getAppBusinessReqDao().getPerson(str_nowPerson);
					if("101".equals(str_processConfig)){
						businessRequest.setProcinstType("01");
						
					}else{
						if("优化立项流程".equals(str_processConfig)){
							businessRequest.setProjectApply("01");
						}else if("优化支撑流程".equals(str_processConfig)){
							businessRequest.setProjectApply("02");
						}else{
							
						}
						businessRequest.setProcinstType("02");
						str_processConfig="103";
					}
					 processConfig=getProcessPvcService().getFlowPvcDao().getProcessConfig(str_processConfig);
					 processConfigDetail=getProcessPvcService().getFlowPvcDao().getProcessConfigDetail(str_processCongigDetail, str_processConfig);
					
					//this.getAppBusinessReqDao().save(businessRequest);
					TransactProcessStatus processStatus=new TransactProcessStatus();
					processStatus.setCurrProcessConfig(processConfig);
					processStatus.setCurrProcessConfigDetail(processConfigDetail);
					processStatus.setState("active-root");
					processStatus.setBegin(new Date());
					processStatus.setCurrTransactor(nowPerson.getName());
					
					ProcTranRecord procTranRecord=new ProcTranRecord();
					procTranRecord.setProcessConfig(processConfig);
					procTranRecord.setProcessStep(processConfigDetail);
					procTranRecord.setTransactor(nowPerson);
					procTranRecord.setReceiveTime(new Date());
					procTranRecord.setState("active");
					procTranRecord.setTaskType("01");
					List<ProcTranRecord> listProcTranRecord=new ArrayList<ProcTranRecord>();
					listProcTranRecord.add(procTranRecord);
					businessRequest.setImportance(sheet.getCell(9, j).getContents());
					businessRequest.setType(sheet.getCell(10,j).getContents());
					User user=getProcessPvcService().getFlowPvcDao().getUserByPerson(nowPerson.getId());
					
					
					String reqProg=sheet.getCell(11, j).getContents();
					businessRequest.setDaiType(sheet.getCell(12, j).getContents());
					if(reqProg!=null&&!"".equals(reqProg)){
						ReqProgress reqProgress=new ReqProgress();
						reqProgress.setReqProg(reqProg);
						reqProgress.setBegin(DateUtil.strToDate("2010-05-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						reqProgress.setCreateTime(DateUtil.strToDate("2010-05-01"+" 08:00:00","yyyy-MM-dd HH:mm:ss"));
						reqProgress.setPercent("100");
						reqProgress.setCreator(user.getUserID());
						reqProgress.setProcessConfigDetail(processConfigDetail);
						this.saveFlowAndBsData(businessRequest, processStatus, listProcTranRecord,reqProgress);
					}else{
						this.saveFlowAndBsData(businessRequest, processStatus, listProcTranRecord,null);
					}
				}
			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    
    public void saveFlowAndBsData(BusinessRequest businessRequest,TransactProcessStatus processStatus,List<ProcTranRecord> listProcTranRecord,ReqProgress reqProgress){
    	this.getAppBusinessReqDao().save(processStatus);
    	Integer procinstId=processStatus.getId();
    	processStatus.setProcinstId(procinstId);
    	this.getAppBusinessReqDao().save(processStatus);
    	businessRequest.setProcinstId(procinstId);
    	this.getAppBusinessReqDao().save(businessRequest);
    	ProcTranRecord procTranRecord=null;
    	for (int i = 0; i < listProcTranRecord.size(); i++) {
    		procTranRecord=listProcTranRecord.get(i);
    		procTranRecord.setProcinstId(procinstId);
    		this.getAppBusinessReqDao().save(procTranRecord);
    		if("active".equals(procTranRecord.getState())){
    			processStatus.setActId(procTranRecord.getId());
    			if(reqProgress!=null){
    	    		
    	    		reqProgress.setRecotdId(procTranRecord.getId().toString());
    	    	}
    		}
		}
    	this.getAppBusinessReqDao().save(processStatus);
    	if(reqProgress!=null){
    		reqProgress.setRequestBusiness(businessRequest);
    		this.getAppBusinessReqDao().save(reqProgress);
    	}
    	
    	
    	//将发起人与产品经理的swimlace写入
    	String assign=businessRequest.getProdPerson().getId().toString();
    	saveBpmSwimlance(assign,procinstId,"startUserSwinRole");
    	saveBpmSwimlance(assign,procinstId,"prodPersonSwim");
    	
    }
    
    private void saveBpmSwimlance(String assign,Integer procinstId,String swnlance){
    	BpmSwimlane bpmSwimlane=new BpmSwimlane();
    	bpmSwimlane.setAssign(assign);
    	bpmSwimlane.setSwinlane(swnlance);
    	bpmSwimlane.setProcinstId(procinstId);
    	this.getAppBusinessReqDao().save(bpmSwimlane);
    }
    
	public AppBusinessReqDao getAppBusinessReqDao() {
		return appBusinessReqDao;
	}
	public void setAppBusinessReqDao(AppBusinessReqDao appBusinessReqDao) {
		this.appBusinessReqDao = appBusinessReqDao;
	}
	public ProcessPvcService getProcessPvcService() {
		return processPvcService;
	}
	public void setProcessPvcService(ProcessPvcService processPvcService) {
		this.processPvcService = processPvcService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}


	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}


	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}


	
    
}
